

#ifndef _L_MEMORY_MANAGER_H_
#define _L_MEMORY_MANAGER_H_

#include "MemDefine.h"

//#ifndef _FORCE_NOT_REDEFINE_NEW_

#ifdef _DEBUG

inline void * __cdecl operator new (size_t len, CSTR filename, int32 linenumber, int asdf) 
{
	return MemAlloc(len,"<object>",filename,linenumber);
}

inline void * __cdecl operator new[] (size_t len, CSTR filename, int32 linenumber, int asdf) 
{
	return MemAlloc(len,"<object>",filename,linenumber);
}

inline void * __cdecl operator new (size_t len) 
{
	return MemAlloc(len,"<object>","Unknown",-1);
}

inline void * __cdecl operator new[] (size_t len) 
{
	return MemAlloc(len,"<object>","Unknown",-1);
}

// declare corresponding dummy delete in case exception handling is enabled
inline void __cdecl operator delete (void *p, CSTR, int32, int) 
{
	MemFree(p);
}

inline void __cdecl operator delete[] (void *p, CSTR, int32, int) 
{
	MemFree(p);
}


inline void __cdecl operator delete (void *p) 
{
	MemFree(p);
}

inline void __cdecl operator delete[] (void *p) 
{
	MemFree(p);
}


template<class T> inline T* ALLOC_template(T *&v, 
										   size_t x, 
   										   CSTR varname, 
										   CSTR filename, 
										   int32 linenumber) 
{
	return v = (T*)MemAlloc(x*sizeof(T),varname,filename,linenumber);
}

template<class T> inline T* REALLOC_template(T *&v, 
											 size_t x, 
											 CSTR varname, 
											 CSTR filename, 
											 int32 linenumber) 
{
	return v = (T*)MemReAlloc(v,x*sizeof(T),varname,filename,linenumber);
}

template<class T> inline T* ALLOCZ_template(T *&v, 
											size_t x, 
											CSTR varname, 
											CSTR filename, 
											int32 linenumber) 
{
	return v = (T*)MemAllocZero(x*sizeof(T),varname,filename,linenumber);
}

#define ALLOC(v,x,d)			ALLOC_template(v,x,#v,__FILE__,__LINE__)
#define REALLOC(v,x,d)			REALLOC_template(v,x,#v,__FILE__,__LINE__)
#define ALLOCZERO(v,x,d)		ALLOCZ_template(v,x,#v,__FILE__,__LINE__)

#define new						new(__FILE__,__LINE__,0)

#else //_DEBUG

inline void * __cdecl operator new (size_t len) 
{
	return MemAlloc(len);
}

inline void * __cdecl operator new[] (size_t len) 
{
	return MemAlloc(len);
}

inline void __cdecl operator delete (void *p) 
{
	MemFree(p);
}

inline void __cdecl operator delete[] (void *p) 
{
	MemFree(p);
}


template<class T> inline T* ALLOC_template(T *&v, size_t x) 
{
	return v = (T*)MemAlloc(x*sizeof(T));
}

template<class T> inline T* REALLOC_template(T *&v, size_t x) 
{
	return v = (T*)MemReAlloc(v,x*sizeof(T));
}

template<class T> inline T* ALLOCZ_template(T *&v, size_t x) 
{
	return v = (T*)MemAllocZero(x*sizeof(T));
}

#define ALLOC(v,x,d)			ALLOC_template(v,x)
#define REALLOC(v,x,d)			REALLOC_template(v,x)
#define ALLOCZERO(v,x,d)		ALLOCZ_template(v,x)

//#define new						new

#endif // _DEBUG

template<class T> inline void FREE_template(T *&v) 
{
	MemFree(v);
}

#define FREE(p)					FREE_template(p)
#define FREEIFUSED(p)			{if (p != NULL) FREE_template(p);}

//#endif //_FORCE_NOT_REDEFINE_NEW_

#endif //_K_MEMORY_MANAGER_H_

